package com.hdax.gulimall.order.controller;


import com.hdax.gulimall.order.entity.OrderEntity;
import com.hdax.gulimall.order.service.MqMessageService;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class Text {


    @Autowired
    private AmqpAdmin amqpAdmin;

    /**
     * 收发消息的组件
     */
    @Autowired
    private RabbitTemplate rabbitTemplate;




    /**
     * 测试发送消息
     */
    @GetMapping("/message")
    public String message(@RequestParam("num") Integer num){
        //交换机
        //列队的路由键
        //发送的消息
        /**
         * 如果我们发送的消息是一个对象，会使用序列化机制，将对象写出去，要求对象必须时间序列化接口
         * 如果想让发送json需要配置json序列化
         */
        for(int i=0;i<num;i++){
            if(i%2==0){
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setBillHeader("dddddddd");
                orderEntity.setOrderSn("ddddddd");
                orderEntity.setBillType(i);
                rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",orderEntity);
            }else{
                OrderEntity orderEntity=new OrderEntity();
                orderEntity.setBillContent(UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",orderEntity);
            }
        }
            return "ok";
    }



    /**
     * 1、创建交换机(Exchange),队列(Queue),Bingding
     * 2、收发消息
     */
    @GetMapping("/a")
    public void craeteExchange(){
       //创建交换机
        //交换机的名称，
        //是否持久化
        //是否自动删除
        DirectExchange directExchange = new DirectExchange("hello-java-exchange",true,false);
        amqpAdmin.declareExchange(directExchange);
    }


    /**
     * 创建队列
     */
    @GetMapping("/aa")
    public void createQueue(){
        //队列的名字
        //1、队列的名称2、是否持久化 4、是否自动删除
        //指定参数
        Queue queue = new Queue("hello.java",true,false,false);
        amqpAdmin.declareQueue(queue);
    }


    /**
     * 将交换机和列队进行绑定
     */
    @GetMapping("/bangind")
    public void createBingding(){
        //  super(arguments);
        //        this.destination = destination;[绑定的交换机，目的地]
        //        this.destinationType = destinationType;[目的地类型]
        //        this.exchange = exchange;[交换机]
        //        this.routingKey = routingKey;【路由键】

        //队列的名称，队列的类型,交换机的名称,路由键
        Binding binding = new Binding("hello.java", Binding.DestinationType.QUEUE,"hello-java-exchange","hello.java",null);
        amqpAdmin.declareBinding(binding);
    }





}
